﻿function CommaFormatted(num, isUsd) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    if (isUsd == 1)
        return (((sign) ? '' : '-') + num + '.' + cents);
    return (((sign) ? '' : '-') + num);
}

function HighlightMenu() {
    var current = $('a[href="' + window.location.pathname + '"]');
    if ($(current).length == 0)
        current = $('a[href="' + window.location.pathname.replace("Edit.aspx", ".aspx") + '"]');
    $(current).addClass('current');
    $(current).closest('ul').css("display", "block");
    $(current).closest('ul').prev().addClass('current');
}

//Object.prototype.nextDivObject = function() {
//	var n = this;
//	do n = n.nextSibling;
//	while (n && n.nodeType != 1 && n.nodeName != "DIV");
//	return n;
//};

function MasterPageLoad() {
    $(".datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd-M-yy'
    });
    $('.checkall').click(function() {
        //Checkall apply for header gridview is input type="checkbox" class="checkall"
        jQuery(this).parents('table:eq(0)').find(':checkbox').attr('checked', this.checked);
    });
    HighlightMenu();

//    var onode = document.getElementById("ReportViewer1_ctl00").nextDivObject();
//    for (var i = 0; i < onode.childNodes.length; i++) {
//        if (onode.childNodes[i].nodeType == 1) {
//            for (var j = 0; j < onode.childNodes[i].childNodes.length; j++) {
//                if (onode.childNodes[i].childNodes[j].nodeType == 1) {
//                    onode.childNodes[i].childNodes[j].style.display = "inline-block";
//                }
//            }
//        }
//    }
 
 

 
//    if ($.browser.safari) { // toolbars appeared on separate lines. 
//        $('[id*=ReportViewer] table').each(function(i, item) {
//            if ($(item).attr('id') && $(item).attr('id').match(/fixedTable$/) != null)
//                $(item).css('display', 'table');
//            else
//                $(item).css('display', 'inline-block');
//        });
//        $('[id*=ReportViewer] table td').css('padding', '0px');
//    }

    // needed when AsyncEnabled=true.
	
	// Make dropdownlist searchable
	//$("select").searchable();
	
	// Change to use chosen
	
	$("select:not(.disable-chosen)").chosen({ disable_search_threshold: 8 });
    //Select except class defaultFocus
	//$("select:not(.defaultFocus)").chosen({disable_search_threshold: 10});
	
	// Fix the auto open of datepicker when dialog open
	$("#dialog .datepicker").datepicker("disable");
	
	// Intialize dialog for all the element id dialog
	var dlg=$("#dialog").dialog({
		draggable: true,
        resizable: true,
        show: 'Transfer',
        hide: 'Transfer',
        width: 320,
        minHeight: 10,
        minwidth: 10,
		autoOpen: false,
		// Fix the auto open of datepicker when dialog open
		open: function(event, ui) {
            $(".datepicker").datepicker("enable");
        }
	});
	
	//To Fix The Asp.net button not work on dialog
	dlg.parent().appendTo(jQuery("form:first"));
}
